/**
 * Copyright (c) 2006
 *      OpenAVS Developers. All Rights Reserved.
 *
 * Copyright (c) 2005-2006
 *      NSCC. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * @file define.h
 */

#ifndef _AVS_DEFINE_H_
#define _AVS_DEFINE_H_

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*
 * ISO C Standard:  7.16  Boolean type and values  <stdbool.h>
 */
#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#endif

/* 
 * ISO C99: 7.18 Integer types <stdint.h>
 */
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
//header--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
//dingo--------------------------------------------------------

#define DEBUG_MODE  1
//#define PRINTINFO 1
//constant------------------------------------------------------
#define MBSIZE    16
#define INBUFLEN  1024*1024*5

#define AVS_MAX_HORIZONTAL 2000
#define AVS_MAX_VERTICAL   2000

#define MAX_MB_NUM 15625
#define CLIPSIZE   1024
#define TEMP_HALF_PIX_LEN 18
#define TEMP_JPIE_PIX_LEN 9
//return value--------------------------------------------------
#define AVS_NOERROR                   0x00000000
#define AVS_FALSE                     0xffffffff
#define AVS_ERROR                     0xffffffff
#define AVS_CAN_NOT_OPEN_FILE         0xF0000001
#define AVS_NOT_ENOUGH_DATA            0xF0000002
#define AVS_NOT_ENOUGH_MEM            0xF0000003
#define AVS_NOT_VALID_PIC_DATA        0xF0000004
#define AVS_INVALID_PIC_START_CODE    0xF0000005

#define THIS_VERSION_NOT_SUPPORT      0xF00000FF

#define TRUE  1
#define FALSE 0

//constant in avs video standard -------------------------------
#define SEQENCE_START_CODE   0X000001B0
#define SEQENCE_END_CODE     0X000001B1
#define USER_DATA_START_CODE 0X000001B2
#define I_FRAME_START_CODE   0X000001B3
#define EXTENSION_START_CODE 0X000001B5 
#define PB_FRAME_START_CODE  0X000001B6
#define EDIT_CODE            0X000001B7

#define SLICE_START_CODE_MIN 0X00000100
#define SLICE_START_CODE_MAX 0X000001AF
#define IS_START_CODE(data) ((data & 0x00FFFFFF) == 0x00010000)
#define IS_SLICE_START_CODE(data)       (((data) >= SLICE_START_CODE_MIN)  \
                                      && ((data) <= SLICE_START_CODE_MAX))

//stream constant-----------------------------------------------
#define BASE_PROFILE    0X20

#define LEVEL40         0X20
#define LEVEL42         0x22
#define LEVEL60         0x40
#define LEVEL62         0x42

#define CHROMA420       0X01
#define CHROMA422       0X10

#define I_IMG           0
#define P_IMG           1
#define B_IMG           2

// Intra Mode
#define I_8x8           0

//P Mb mode
#define P_Skip          1
#define P_16x16         2
#define P_16x8          3
#define P_8x16          4
#define P_8x8           5

//B Mb mode
#define B_Skip          6
#define B_Direct_16x16  7
#define B_Fwd_16x16     8
#define B_Bck_16x16     9
#define B_Sym_16x16     10
#define B_Fwd_Fwd_16x8  11
#define B_Fwd_Fwd_8x16  12
#define B_Bck_Bck_16x8  13
#define B_Bck_Bck_8x16  14
#define B_Fwd_Bck_16x8  15
#define B_Fwd_Bck_8x16  16
#define B_Bck_Fwd_16x8  17
#define B_Bck_Fwd_8x16  18
#define B_Fwd_Sym_16x8  19
#define B_Fwd_Sym_8x16  20
#define B_Bck_Sym_16x8  21
#define B_Bck_Sym_8x16  22
#define B_Sym_Fwd_16x8  23
#define B_Sym_Fwd_8x16  24
#define B_Sym_Bck_16x8  25
#define B_Sym_Bck_8x16  26
#define B_Sym_Sym_16x8  27
#define B_Sym_Sym_8x16  28
#define B_8x8           29      

// B_8x8 Part Mode
#define SB_Direct_8x8   30
#define SB_Fwd_8x8      31
#define SB_Bck_8x8      32
#define SB_Sym_8x8      33

//IntraLumaPredMode
#define Intra_8x8_Vertical      0
#define Intra_8x8_Horizontal    1
#define Intra_8x8_DC            2
#define Intra_8x8_Down_Left     3
#define Intra_8x8_Down_Right    4

//IntraChromaPredMode
#define Intra_Chroma_DC         0
#define Intra_Chroma_Horizontal 1
#define Intra_Chroma_Vertical   2
#define Intra_Chroma_Plane      3


#define PREDDIRFWD              1
#define PREDDIRBWD              0
#define PREDDIRSYM              2
// Vlc constant-------------------------------------------------
#define CODE2D_ESCAPE_SYMBOL 59


//type define---------------------------------------------------
#ifndef AVS_BYTE_DEFINED
#define AVS_BYTE_DEFINED

typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef char int8_t;
typedef int int32_t;
typedef short int16_t;

typedef uint8_t		BYTE;
typedef uint32_t	DWORD;
#endif

typedef uint32_t	AVS_DWORD;
typedef uint8_t		AVS_BYTE;
typedef int16_t		AVS_SHORT;
typedef uint32_t	AVS_LONG;
typedef uint32_t	AVS_UINT;
typedef uint16_t	AVS_USHORT;

#define AVS_I8		int8_t
#define AVS_U8		uint8_t
#define AVS_I16		int16_t
#define AVS_U16		uint16_t
#define AVS_I32		int32_t
#define	AVS_U32		uint32_t
#define AVS_I64		int64_t
#define AVS_U64		uint64_t

// ycb typedef bool		AVS_BOOL;
typedef int		AVS_BOOL;
typedef int32_t		AVS_INT;

typedef float 		AVS_FLOAT;
typedef double 		AVS_DOUBLE;

typedef uint32_t 	AVS_HRESULT;

//enum -------------------------------------------------------
typedef enum {
  SE_HEADER,
  SE_PTYPE,
  SE_MBTYPE,
  SE_REFFRAME,
  SE_INTRAPREDMODE,
  SE_MVD,
  SE_CBP_INTRA,
  SE_LUM_DC_INTRA,
  SE_CHR_DC_INTRA,
  SE_LUM_AC_INTRA,
  SE_CHR_AC_INTRA,
  SE_CBP_INTER,
  SE_LUM_DC_INTER,
  SE_CHR_DC_INTER,
  SE_LUM_AC_INTER,
  SE_CHR_AC_INTER,
  SE_DELTA_QUANT_INTER,
  SE_DELTA_QUANT_INTRA,
  SE_BFRAME,
  SE_EOS,
  SE_MAX_ELEMENTS  //!< number of maximum syntax elements
} SE_type;         // substituting the definitions in elements.h
//structures----------------------------------------------------

typedef struct _VIDEODATA {
  AVS_DWORD dwDistanceIndex;
  AVS_BYTE* y;
  AVS_BYTE* u;
  AVS_BYTE* v;
  AVS_BYTE* reserve;
} VIDEODATA;

typedef struct _MOTIONVECTOR {
  AVS_INT x;
  AVS_INT y;
} MOTIONVECTOR;

typedef struct _MBINFO {
  AVS_DWORD dwMbType;
  AVS_DWORD dwMvNum; 
  AVS_DWORD dwMbPartType[4];           //B_8x8 only
  AVS_DWORD dwCBPCodeNum;
  AVS_DWORD  bPredModeFlag[4];         //I_8x8 only
  AVS_INT   iIntraLumaPredMode[4];     //I_8x8 only
  AVS_INT   iIntraLumaPredModeNew[4];
  AVS_DWORD dwintraChromaPredMode;
  AVS_DWORD dwintraChromaPredMode422;  //Chroma 4:2:2 only

  AVS_DWORD dwMbReferenceIndex[4];
  AVS_DWORD dwMbReferenceIndexBw[4];
  
  AVS_INT   iMvDiffX[4];
  AVS_INT   iMvDiffY[4];
  AVS_DWORD dwWeightingPrediction;
  AVS_DWORD  bHasCbp;
  AVS_DWORD dwCbp;
  AVS_DWORD dwCbp422;
  AVS_INT   iMbQpDelta;
  AVS_INT   predDir[4];    //fwd: 1, bwd: 0, sym: 2
  AVS_INT   iBlockDistance[4];
  AVS_INT   iBlockDistanceBw[4];
  MOTIONVECTOR mv[4];
  MOTIONVECTOR mvBw[4];

  AVS_DWORD dwMbQp;
} MBINFO;

typedef struct _SEQENCEINFO {
  AVS_DWORD dwWidth;
  AVS_DWORD dwHeight;
  AVS_DWORD dwChromaFormat;
  AVS_DWORD dwAspectRatio;
  AVS_FLOAT fFrameRate;
  AVS_DWORD dwMbWidth;
  AVS_DWORD dwMbHeight;
  AVS_BOOL  bProgressive;
  AVS_DWORD dwProfile;
  AVS_DWORD dwLevel;
  AVS_BOOL  bLowDelay;
} SEQENCEINFO;

typedef struct _IMAGEINFO {
  AVS_DWORD dwImageType;
  AVS_DWORD dwImageType1;
  AVS_DWORD dwPictureDistance;
  AVS_BOOL  bProgressiveFrame;
  AVS_BOOL  bPictureStructure;
  AVS_BOOL  bTopFieldFirst;
  AVS_BOOL  bRepeatFirstField;
  AVS_BOOL  bFixedPictureQp;
  AVS_DWORD dwPictureQp;
  AVS_BOOL  bSkipModeFlag;
  AVS_BOOL  bLoopFilterDisable;
  AVS_INT   iAlphaCOffset;
  AVS_INT   iBetaOffset;
  //pb frame
  AVS_BOOL  bPictureReferenceFlag;
} IMAGEINFO;

typedef struct _SLICEINFO {
  AVS_DWORD  bFixedSliceQp;
  AVS_DWORD dwSliceQp;
  AVS_DWORD LumaScale[4];
  AVS_DWORD LumaShift[4];
  AVS_DWORD ChromaScale[4];
  AVS_DWORD ChromaShift[4];
  AVS_DWORD  bMbWeightingFlag;
} SLICEINFO;

typedef struct _STREAMINFO {
  SEQENCEINFO SeqInfo;
  IMAGEINFO ImgInfo;
  SLICEINFO SlcInfo;
} STREAMINFO;

typedef struct _BWREFINFO {
  AVS_DWORD    dwMbType;
  MOTIONVECTOR mvRef[4];
  AVS_INT      iBlockDistanceRef[4];
  AVS_INT      iDistanceIndexRef[4];
  AVS_INT      PictureStructure;
} BWREFINFO;

#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define min1(a,b) (((a) < (b)) ? (a) : (b))
#define Clip(a) ( (a)<(0) ? (0) : ((a)>(255)?(255):(a)) ) 
#define Median(a,b,c) (a + b + c - min(a, min(b, c)) - max(a, max(b, c)));
#define sign1(x) (x < 0 ? -1 : 1)
#define sign(x) (x < 0 ? -1 : 1)
#define Clip3(min,max,val) (((val)<(min))?(min):(((val)>(max))?(max):(val)))

#if __GNUC_MINOR__ >= 1
// ycb # define inline         inline          __attribute__((always_inline))
//# define __inline__     __inline__      __attribute__((always_inline))
//# define __inline       __inline        __attribute__((always_inline))
# define inline 
# define __inline__ 
# define __inline 
#endif

#endif

